package seminar;

import group.SystemGroup;
import group.UserGroup;
import gui.GUI;

import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

import role.Role;
import student.Student;

public class Seminar
{
	private static SystemGroup systemgroup = new SystemGroup();
	private static ArrayList<UserGroup> usergroups = new ArrayList<UserGroup>();

	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run()
			{
				try
				{
					// seminar.add();
					GUI frame = new GUI();
					frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}
	
	public static void addDefaultStudent()
	{
		systemgroup.addStudent("", "", 0, "", "");
	}

	public static void read_txt(File file)
	{

		try
		{
			@SuppressWarnings("resource")
			BufferedReader in = new BufferedReader(new FileReader(file));
			String zeile = null;
			while ((zeile = in.readLine()) != null)
			{
				String[] temp = zeile.replaceAll(" ", "").split(";|,|#");
				// Zeile wird von Leerzeichen befreit -> String wird nach
				// Trennzeichen [, oder ; oder #] getrennt.
				if (temp.length == 6)
				{
					systemgroup.addStudent(temp[1], temp[2],
							Integer.parseInt(temp[3]), temp[5], temp[4]);
				}
			}

		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return;
	}

	public static void writeTxt(String file) throws IOException
	{
		FileWriter fw = new FileWriter(file, false);
		BufferedWriter ausgabe = new BufferedWriter(fw);
		/*
		 * ausgabe.write(text); ausgabe.newLine(); ausgabe.close();
		 */

		ausgabe.write("Studenten ohne Gruppenzugehörigkeit:");
		ausgabe.newLine();
		ausgabe.write("keine Rollen vorhanden:   Vorname:                  Nachname:                 MatrNr:    Fachber.:    e-Mail:");
		ausgabe.newLine();
		ausgabe.write("                          -----------------------   -----------------------   --------   ----------   -------");
		ausgabe.newLine();
		Iterator<Student> inter = systemgroup.getMembers().iterator();

		while (inter.hasNext())
		{
			Student s = inter.next();
			String text = "                        # ";
			Integer MatrNr;

			if (s.getFirstName().length() > 20)
			{
				text = text + s.getFirstName().substring(0, 20) + "... ";
			} else
			{
				text += s.getFirstName();
				for (int i = s.getFirstName().length(); i <= 23; i++)
				{
					text = text + " ";
				}
			}
			text = text + "# ";

			if (s.getLastName().length() > 20)
			{
				text = text + s.getLastName().substring(0, 20) + "... ";
			} else
			{
				text = text + s.getLastName();
				for (int i = s.getLastName().length(); i <= 23; i++)
				{
					text = text + " ";
				}
			}
			text = text + "# ";

			MatrNr = s.getMatrNr();
			if (MatrNr.toString().length() > 8)
			{
				text = text + MatrNr.toString().substring(0, 8) + " ";
			} else
			{
				text = text + MatrNr.toString();
				for (int i = MatrNr.toString().length(); i <= 8; i++)
				{
					text = text + " ";
				}
			}
			text = text + "# ";

			if (s.getBranch().length() > 7)
			{
				text = text + s.getBranch().substring(0, 7) + "... ";
			} else
			{
				text = text + s.getBranch();
				for (int i = s.getBranch().length(); i <= 10; i++)
				{
					text += " ";
				}
			}
			text = text + "# ";

			text = text + s.getMail();

			ausgabe.write(text);
			ausgabe.newLine();
		}
		ausgabe.newLine();
		Iterator<UserGroup> inter_us = usergroups.iterator();

		while (inter_us.hasNext())
		{
			UserGroup userGruppe = inter_us.next();
			ausgabe.newLine();
			ausgabe.write(userGruppe.getName() + ":");
			ausgabe.newLine();
			ausgabe.write("Rolle:                    Vorname:                  Nachname:                 MatrNr:    Fachber.:    e-Mail:");
			ausgabe.newLine();
			ausgabe.write("-----------------------   -----------------------   -----------------------   --------   ----------   -------");
			ausgabe.newLine();
			
			ArrayList<Role> roles = userGruppe.getRoles();
			Iterator<Role> interRole = roles.iterator();

			while (interRole.hasNext())
			{
				Role r = interRole.next();
				String text = "";
				Integer MatrNr;
				if (r.getName().length() > 20)
				{
					text = text + r.getName().substring(0, 20) + "... ";
				} else
				{
					text += r.getName();
					for (int i = r.getName().length(); i <= 23; i++)
					{
						text = text + " ";
					}

				}
				text = text + "# ";
				
				if (r.getStudent().getFirstName().length() > 20)
				{
					text = text + r.getStudent().getFirstName().substring(0, 20) + "... ";
				} else
				{
					text += r.getStudent().getFirstName();
					for (int i = r.getStudent().getFirstName().length(); i <= 23; i++)
					{
						text = text + " ";
					}

				}
				text = text + "# ";

				if (r.getStudent().getLastName().length() > 20)
				{
					text = text + r.getStudent().getLastName().substring(0, 20) + "... ";
				} else
				{
					text = text + r.getStudent().getLastName();
					for (int i = r.getStudent().getLastName().length(); i <= 23; i++)
					{
						text = text + " ";
					}
				}
				text = text + "# ";

				MatrNr = r.getStudent().getMatrNr();
				if (MatrNr.toString().length() > 8)
				{
					text = text + MatrNr.toString().substring(0, 8) + " ";
				} else
				{
					text = text + MatrNr.toString();
					for (int i = MatrNr.toString().length(); i <= 8; i++)
					{
						text = text + " ";
					}
				}
				text = text + "# ";

				if (r.getStudent().getBranch().length() > 7)
				{
					text = text + r.getStudent().getBranch().substring(0, 7) + "... ";
				} else
				{
					text = text + r.getStudent().getBranch();
					for (int i = r.getStudent().getBranch().length(); i <= 10; i++)
					{
						text += " ";
					}
				}
				text = text + "# ";

				text = text + r.getStudent().getMail();

				ausgabe.write(text);
				ausgabe.newLine();
			}

		}

		ausgabe.close();
	}

	public static void distributeStudents()
	{
		Iterator<UserGroup> iter_grup = usergroups.iterator();
		while (iter_grup.hasNext())
		{
			UserGroup userGr = iter_grup.next();
			while (userGr.hasSpace() && (systemgroup.getMembers().size() > 0))
			{
				userGr.addStudent(systemgroup.getStudent());
			}
		}
	}

	public static SystemGroup getSystemgroup()
	{
		return systemgroup;
	}

	public static ArrayList<UserGroup> getUsergoups()
	{
		return usergroups;
	}

	public static ArrayList<Student> getAllMembers()
	{
		ArrayList<Student> completegroup = new ArrayList<Student>();
		Iterator<Student> systemIter_stud = systemgroup.getMembers().iterator();
		while (systemIter_stud.hasNext())
		{
			Student s = systemIter_stud.next();
			completegroup.add(s);
		}
		Iterator<UserGroup> iter_grup = usergroups.iterator();
		while (iter_grup.hasNext())
		{
			UserGroup userGr = iter_grup.next();
			Iterator<Student> iter_stud = userGr.getMembers().iterator();
			while (iter_stud.hasNext())
			{
				Student s = iter_stud.next();
				completegroup.add(s);
			}
		}
		return completegroup;
	}

	/**
	 * Löscht den übergebenen Studenten
	 * 
	 * @param student
	 * @return true, wenn gelöscht, false, sonst
	 */
	public static boolean removeStudent(Student student)
	{
		if (systemgroup.removeStudent(student))
			return true;
		ListIterator<UserGroup> it = usergroups.listIterator();
		while (it.hasNext())
		{
			if (it.next().removeStudent(student))
				return true;
		}
		return false;

	}

	public static UserGroup getUserGroupByName(String groupname)
	{

		Iterator<UserGroup> iter = getUsergoups().iterator();
		UserGroup tmpug = null;
		while (iter.hasNext())
		{
			tmpug = iter.next();
			if (tmpug.getName().equals(groupname))
				break;

		}
		return tmpug;
	}
}